<?php
namespace app\admin\controller;
use app\BaseController;
use app\common\model\WebViewModel;
use app\common\OpenApi;
class Webview extends BaseController{
    
    public function list(){
        $where=[
            ['firm_id','=',$this->request->firm_id],
            ['is_delete','=',0]
        ];
        if(!empty($keyword)){
            $where[] = ['name','LIKE','%'.$keyword.'%'];
        }
        $list = WebViewModel::where($where)
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->order('sort','desc')->select();
        $total = WebViewModel::where($where)->count('web_id');
        return success([
            'list' => $list,
            'total' => $total
        ]);
    }
    
    public function save(){
        $web_id = $this->request->param('web_id/d',0);
        $url = $this->request->param('url/s','');
        $name = $this->request->param('name/s','');
        $sort = $this->request->param('sort/d',0);
        if(empty($url)){
            return error('请输入url链接');
        }
        if(empty($name)){
            return error('请输入链接名称');
        }
        if(empty($web_id)){
            $WebViewModel = new WebViewModel();
            $WebViewModel->firm_id = $this->request->firm_id;
        }else{
            if(null === $WebViewModel = WebViewModel::find($web_id)){
                return error('请选择要编辑的地址');
            }
            if($WebViewModel->firm_id != $this->request->firm_id){
                return error('请选择要编辑的地址');
            }
        }
        $WebViewModel->name = $name;
        $WebViewModel->url = $url;
        $WebViewModel->sort = $sort;
        $WebViewModel->save();
        return success('操作成功');
    }
    
    public function check(){
        $web_id = $this->request->param('web_id/d',0);
        
        if(empty($web_id)){
            return error('错误操作');
        }
        if(null === $WebViewModel = WebViewModel::find($web_id)){
            return error('内容不存在');
        }
        if($WebViewModel->firm_id != $this->request->firm_id){
             return error('没有权限');
        }
        $data = [
            'action' => 'add',
            'webviewdomain' => $WebViewModel->url
        ];
        $OpenApi = new OpenApi($this->request->firm_id);
        $res = $OpenApi->openApiPost('miniapp/setWebViewDomain',$data);
        if($res['code'] == 1){
            $WebViewModel->status = 4;
            $WebViewModel->msg = '[验证未通过]'.$res['msg'];
        }else{
            $WebViewModel->status = 1;
            $WebViewModel->msg = '验证通过';
        }
        
        if($WebViewModel->save() && $WebViewModel->status == 1){
            return success('验证通过');
        }else{
            return error('验证失败');
        }
    }
    
    public  function delete(){
        $web_id= $this->request->param('web_id/d',0);
        if(empty($web_id)){
            return error('删除失败');
        } 
        if(null === $WebViewModel = WebViewModel::find($web_id)){
            return error('不存在');
        }
        if($WebViewModel->firm_id != $this->request->firm_id){
            return error('无权限');
        }
        
        $data = [
            'action' => 'delete',
            'webviewdomain' => $WebViewModel->url
        ];
        
        $OpenApi = new OpenApi($this->request->firm_id);
        $res = $OpenApi->openApiPost('miniapp/setWebViewDomain',$data);
        if($res['code'] == 1){
            return error('删除失败');
        }
        
        $WebViewModel->is_delete = 1;
        $WebViewModel->save();
        return success('操作成功');
    }
    
    public function download(){
        $OpenApi = new OpenApi($this->request->firm_id);
        $res = $OpenApi->openApiPost('miniapp/getWebWiewDomainConfirmfile');
        if(empty($res) || $res['code'] == 1){
            return error($res['msg'] ?? '错误');
        }
        
        $info = $res['data'];
        
        // 创建一个文件
        $file = $info['file_name'];
        $fp = fopen($file, 'w');
        fwrite($fp, $info['file_content']);
        fclose($fp);
        
        // 设置文件类型和大小
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="' . $file . '"');
        header('Content-Length: ' . filesize($file));
        
        // 输出文件内容
        readfile($file);
        
        // 删除文件
        unlink($file);

    }
    
    
    
}